package com.zui.entity.product;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;

import com.sun.istack.internal.NotNull;
import com.zui.entity.BaseAssignedIdEntity;

@Entity
@Table(name = "category")
public class Category extends BaseAssignedIdEntity {
	private static final long serialVersionUID = 1L;
	public static final String CATEGORY_BIKES = "BIKES";

	private String name;
	private String nameEn;
	private Category primaryParentCategory;
	private CategoryType categoryType;
	private Set<CategoryRollup> childCategoryRollup = new LinkedHashSet<CategoryRollup>();
	private Set<CategoryRollup> parentCategoryRollup = new LinkedHashSet<CategoryRollup>();
	private Set<ProductCategory> childProducts = new LinkedHashSet<ProductCategory>();
	private Set<FeatureCategoryAppl> featureCategoryAppl = new LinkedHashSet<FeatureCategoryAppl>();
	
	
	@Transient
	public Set<Category> getChildCategories() {
		Iterator<CategoryRollup> it = childCategoryRollup.iterator();
		Set<Category> children = new LinkedHashSet<Category>();
		while (it.hasNext())
			children.add(it.next().getCategory());
		return children;
	}	
	
	/*------------------------------
	 * getters and annotations
	 --------------------------------*/	

	@Column(length = 50) @NotNull
	public String getName() {return name;}
	@Column(length = 50) @NotNull
	public String getNameEn() {return nameEn;}
	@ManyToOne() @JoinColumn(name="primaryParentCategoryId")
	public Category getPrimaryParentCategory() {return primaryParentCategory;}

	@OneToMany(mappedBy = "parentCategory", fetch = FetchType.LAZY,cascade = {CascadeType.ALL})
	@OrderBy(value = "sequenceNum")
	@JSON(serialize = false)
	public Set<CategoryRollup> getChildCategoryRollup() {return childCategoryRollup;}

	@OneToMany(mappedBy = "category", fetch = FetchType.LAZY,cascade = {CascadeType.ALL})
	@OrderBy(value = "sequenceNum")
	@JSON(serialize = false)
	public Set<CategoryRollup> getParentCategoryRollup() {
		return parentCategoryRollup;
	}
	@ManyToOne
	@JoinColumn(name="categoryTypeId")
	public CategoryType getCategoryType() {
		return categoryType;
	}
	@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
	@JSON(serialize = false)
	public Set<ProductCategory> getChildProducts() {
		return childProducts;
	}
	
	
	@OneToMany(mappedBy = "category", fetch = FetchType.LAZY,cascade = {CascadeType.ALL})
	@OrderBy(value = "sequenceNum")
	@JSON(serialize = false)
	public Set<FeatureCategoryAppl> getFeatureCategoryAppl() {
		return featureCategoryAppl;
	}

	/*------------------------------
	 * setters
	 --------------------------------*/
	public void setName(String name) {
		this.name = name;
	}

	public void setFeatureCategoryAppl(Set<FeatureCategoryAppl> featureCategoryAppl) {
		this.featureCategoryAppl = featureCategoryAppl;
	}
	public void setNameEn(String nameEn) {
		this.nameEn = nameEn;
	}
	public void setPrimaryParentCategory(Category primaryParentCategory) {
		this.primaryParentCategory = primaryParentCategory;
	}
	public void setChildCategoryRollup(Set<CategoryRollup> childCategoryRollup) {
		this.childCategoryRollup = childCategoryRollup;
	}

	public void setParentCategoryRollup(Set<CategoryRollup> parentCategoryRollup) {
		this.parentCategoryRollup = parentCategoryRollup;
	}
	public void setCategoryType(CategoryType categoryType) {
		this.categoryType = categoryType;
	}
	public void setChildProducts(Set<ProductCategory> childProducts) {
		this.childProducts = childProducts;
	}

}
